package com.yqs.JavaWeb.util;

import javax.sql.DataSource;
import java.sql.*;

/**
 * @project: JavaWeb
 * @package: com.yqs.JavaWeb.util
 * @Description:
 * @Since: jdk版本号
 * @Author: 逝风岁月
 * @Email: 2637182317@qq.com
 * @Date: 2024-09-11 17:39:02
 */
public class DBConnection {

    private static final String URL = "jdbc:mysql://localhost:3306/d1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true";
    private static final String USER = "root"; // 替换为实际的用户名
    private static final String PASSWORD = "123456"; // 替换为实际的密码

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        Connection conn = null;
        try{
             conn  = DriverManager.getConnection(URL, USER, PASSWORD);
        }catch (Exception e){
            e.printStackTrace();
        }
        return conn;
    }


    public static void closeAll(Connection conn, PreparedStatement pre, ResultSet rs){
        try{
            if(rs != null){
                rs.close();
            }
            if( pre != null){
                pre.close();
            }
            if(conn != null){
                conn.close();
            }
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
